<?php

namespace app\api\server\user;


use app\api\server\BaseServer;
use app\common\exception\FailedException;
use app\common\library\Arr;
use app\common\library\Show;
use app\common\model\User;

class LoginServer extends BaseServer
{
    /**
     * Notes:账号密码登录
     * @param $params
     * @return array
     */
    public static function phoneLogin($params)
    {
        try {
            $user = User::checkBaseInfo([['phone','=',$params['phone']]],append:['status_text'],msg:"账号不存在");
            if($user['status'] != User::STATUS_NORMAL){
                throw new FailedException("账号状态异常");
            }
            if ($user['password'] != User::createPwd($params['password'],$user['salt'])) {
                throw new FailedException("密码错误");
            }
            return self::login($user);
        } catch (\Throwable $exception) {
            return Show::error($exception->getMessage());
        }
    }

    /**
     * Notes:登录
     * @return array
     */
    public static function login($user)
    {
        try {
            $token = User::jwt($user);
            return Show::success(Arr::arrayParameterValues($token,"token_type,access_token"));
        } catch (\Throwable $exception) {
            return Show::error($exception->getMessage());
        }
    }
}